package cn.sourcespro.shiro.controller;

import cn.sourcespro.shiro.crudparams.dto.AddSysPermissionDto;
import cn.sourcespro.shiro.entity.SysPermission;
import cn.sourcespro.shiro.service.ShiroService;
import cn.sourcespro.shiro.service.SysPermissionService;
import cn.sourcespro.shiro.service.impl.ShiroServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * security
 *
 * @author zhanghaowei
 * @date 2018/7/13
 */
@RestController
@RequestMapping("sysPerms")
public class SysPermsController {

    @Autowired
    private SysPermissionService sysPermissionService;
    @Autowired
    private ShiroService shiroService;
    @Autowired
    private ShiroFilterFactoryBean shiroFilterFactoryBean;

    @PostMapping("add")
    String add(@RequestBody AddSysPermissionDto dto){
        SysPermission record = new SysPermission();
        record.setUrl(dto.getUrl());
        record.setPerms(dto.getPerms());
        record.setSort(dto.getSort());
        sysPermissionService.insert(record);
        shiroService.updatePermission(shiroFilterFactoryBean);
        return "ok";
    }

    @GetMapping("list")
    List<SysPermission> list(){
        return sysPermissionService.findAll();
    }

    @DeleteMapping("{id}")
    String list(@PathVariable("id") Integer id){
        sysPermissionService.delete(id);
        return "ok";
    }

}
